package com.sjgl.esdn.service.impl;

import com.sjgl.esdn.domain.entity.FileBrowse;
import com.sjgl.esdn.domain.entity.FileDownload;
import com.sjgl.esdn.domain.entity.FileManagement;
import com.sjgl.esdn.domain.entity.FileOff;
import com.sjgl.esdn.mapper.FileManageMapper;
import com.sjgl.esdn.mapper.UserMapper;
import com.sjgl.esdn.service.FileManageService;
import io.lettuce.core.dynamic.annotation.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class FileManageServiceImpl implements FileManageService {

    @Autowired
    private FileManageMapper fileManageMapper;

    @Override
    public List<FileManagement> showAll(){
        List<FileManagement> list = fileManageMapper.fileAll();
        for (FileManagement fileManagement : list) {
            Long userId = fileManagement.getCreateBy();
            Long deptId = fileManageMapper.findDeptidByuserid(userId);
            fileManagement.setLeaderName(fileManageMapper.findLeader(deptId));
            Long fileId = fileManagement.getFileId();
            fileManagement.setDeptName(fileManageMapper.findDeptnameBydeptid(deptId));
            fileManagement.setAbandonName(fileManageMapper.findAbandonName(fileId));
            fileManagement.setAbandonDept(fileManageMapper.findAbandonDeptname(fileManageMapper.findAbandonDept(fileId)));
            if(fileManagement.getFileType()==0){
                fileManagement.setType("私有");
            }
            if(fileManagement.getFileType()==1){
                fileManagement.setType("公共");
            }
            if(fileManagement.getFileType()==2){
                fileManagement.setType("部分可见");
            }
            if(fileManagement.getFileType()==-1){
                fileManagement.setType("下架");
            }
        }
        return list;
    }

    @Override
    public List<FileBrowse> showAllBrowse(Long fileId) {
        List<FileBrowse> list=fileManageMapper.fileAllBrowse(fileId);
        for (FileBrowse fileBrowse : list){
            Long userId = fileBrowse.getUserId();
            Long deptId = fileManageMapper.findDeptidByuserid(userId);
            fileBrowse.setLeader(fileManageMapper.findLeader(deptId));
        }
        return list;
    }

    @Override
    public List<FileDownload> showAllDownload(Long fileId){
        List<FileDownload> list=fileManageMapper.fileAllDownload(fileId);
        for (FileDownload fileDownload :list){
            Long userId = fileDownload.getUserId();
            Long deptId = fileManageMapper.findDeptidByuserid(userId);
            fileDownload.setLeader(fileManageMapper.findLeader(deptId));
        }
        return list;
    }

    @Override
    public List<FileManagement> fileSearch(String keyWord){
        List<FileManagement> list = fileManageMapper.fileSearch(keyWord);
        for (FileManagement fileManagement : list) {
            Long userId = fileManagement.getCreateBy();
            Long deptId = fileManageMapper.findDeptidByuserid(userId);
            fileManagement.setLeaderName(fileManageMapper.findLeader(deptId));
            Long fileId = fileManagement.getFileId();
            fileManagement.setDeptName(fileManageMapper.findDeptnameBydeptid(deptId));
            fileManagement.setAbandonName(fileManageMapper.findAbandonName(fileId));
            fileManagement.setAbandonDept(fileManageMapper.findAbandonDeptname(fileManageMapper.findAbandonDept(fileId)));
            if(fileManagement.getFileType()==0){
                fileManagement.setType("私有");
            }
            if(fileManagement.getFileType()==1){
                fileManagement.setType("公共");
            }
            if(fileManagement.getFileType()==2){
                fileManagement.setType("部分可见");
            }
            if(fileManagement.getFileType()==-1){
                fileManagement.setType("下架");
            }
        }
        return list;
    }

    @Override
    public List<FileBrowse> fileBrowse(String keyWord,Long fileId){
        List<FileBrowse> list=fileManageMapper.fileBrowse(keyWord,fileId);
        for (FileBrowse fileBrowse : list) {
            Long userId = fileBrowse.getUserId();
            Long deptId = fileManageMapper.findDeptidByuserid(userId);
            fileBrowse.setLeader(fileManageMapper.findLeader(deptId));
        }
        return list;
    }

    @Override
    public List<FileDownload> fileDownload(String keyWord,Long fileId){
        List<FileDownload> list=fileManageMapper.fileDownload(keyWord,fileId);
        for (FileDownload fileDownload : list) {
            fileDownload.setLeader(fileManageMapper.findLeader(fileManageMapper.findDeptid2(fileId)));
        }
        return list;
    }

    @Override
    public int updateFileType(Long fileId, String abandonReason, Date abandonTime, Long abandonId){
        return fileManageMapper.updateFileType(fileId, abandonReason,abandonTime, abandonId);
    }
}
